package com.isoft.jms.both;

import javax.jms.Queue;
import javax.jms.Topic;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

/**
 * 同时发送两种模式的消息
 * @Author yigeyanse@hotmail.com
 * @Date 2017年9月6日 上午9:07:44
 */
@Component
@Configuration
public class BothProducer {

	@Value("${spring.activemq.open}")
	private boolean open;

	@Autowired
	private Queue queue;

	@Autowired
	private Topic topic;

	/**
	 * 也可以注入JmsTemplates,JmsMessagingTemplate对JmsTemplates进行了封装
	 */
	@Autowired
	private JmsMessagingTemplate jmsTemplates;

	/**
	 * 每3s执行1次
	 */
	@Scheduled(fixedDelay = 3000)
	public void send3() {
		if (!open)
			return;
		
		this.jmsTemplates.convertAndSend(this.queue, "天王盖地虎");
		System.out.println("Producer发送的消息为:" + "天王盖地虎");
	}

	/**
	 * 每6s执行1次
	 */
	@Scheduled(fixedDelay = 6000)
	public void send6() {
		if (!open)
			return;
		
		this.jmsTemplates.convertAndSend(this.topic, "宝塔镇河妖");
		System.out.println("Producer发送的消息为:" + "宝塔镇河妖");
	}
}
